package servlet.admin.music;

import com.alibaba.fastjson.JSON;
import pojo.Result;
import pojo.Songs;
import service.MusicMngService;
import service.impl.MusicMngServiceImpl;
import utils.JsonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.stream.Collectors;

/**
 * 作者：杨晋
 */

@WebServlet("/admin/editSongServlet")
public class EditSongServlet extends HttpServlet {
    MusicMngService musicMngService = new MusicMngServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userData = req.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
        Songs songs = JsonUtils.jsonToPojo(userData, Songs.class);
        Result result = new Result();
        if (songs == null) {
            result = Result.error("歌名为空");
            // 将 Result 对象转换为 JSON 字符串
            String jsonResponse = JSON.toJSONString(result);
            // 将 JSON 字符串作为响应发送给前端
            resp.getWriter().write(jsonResponse);
        }
        boolean flag = musicMngService.updateSong(songs);
        if (flag) {
            result = Result.success();
        } else {
            result = Result.error("修改失败");
        }
        resp.getWriter().write(JSON.toJSONString(result));
    }
}